<?php
/**
 * ============================================================================
 * 版权所有 2017-2018 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 */
namespace addon\mall;

use app\common\controller\AddonBase;

use app\common\dao\AddonInterface;

use tpfcore\Core;

use think\Db;

class Mall extends AddonBase implements AddonInterface
{
    private $addonName="商城模块";
    /**
     * 插件安装
     */
    public function addonInstall()
    {
        try{
            $this->addonInstallBefore();

            return [RESULT_SUCCESS, $this->addonName.'安装成功'];

        }catch(\Exception $e){

            throw new \Exception($e->getMessage());

        }
        
    }
    
    /**
     * 插件卸载
     */
    public function addonUninstall()
    {   
        try{
            property_exists($this, 'type') && $this->type=='release'?$this->addonReleaseBefore():$this->addonUninstallBefore();

            return [RESULT_SUCCESS, $this->addonName.'卸载成功'];

        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    
    /**
     * 插件基本信息
     */
    public function addonInfo()
    {
        return [
            'name' => 'mall', 
            'title' => $this->addonName, 
            'describe' => '商城插件模块，相对2.2版本升级商品管理、订单管理、财务管理', 
            'author' => 'yaosean', 
            'version' => '2.3' ,
            'require'=>'>=3.1', 
            'type'=>'module',
            "config" => false
        ];
    }
    
    /**
     * 插件配置信息
     */
    public function addonConfig()
    {
        return [];
    }
    /**
    * 完全卸载
    */
    public function addonReleaseBefore(){
        $drop_tables=['cart','seller'];
        try{
            $this->addonUninstallBefore();
            foreach ($drop_tables as $key => $value) {
                Db::execute("DROP TABLE IF EXISTS ".DB_PREFIX.$value.";");
            }
        }catch(\Exception $e){
            throw new \Exception("执行错误".$e->getMessage());
        } 
    }
    /**
     * 插件安装前的一些操作
     */
    public function addonInstallBefore(){
        try{
            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='商城管理' or name ='商城模块' limit 1");

            if(empty($result)){
                Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商城模块', 'mall', '', '', '1', '', '1', '', '', '4', '0');");
                $parentid=Db::getLastInsID();
            }else{
                $parentid=$result[0]['id'];
            }
            
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('订单管理', 'mall', '', '', '1', '', '1', '', 'clipboard', '0', '$parentid');");
            
            $parentid_order=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('订单列表', 'mall', 'order', 'index', '1', '', '1', '', '', '0', '$parentid_order');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('发货清单', 'mall', 'order', 'ship_list', '1', '', '1', '', '', '0', '$parentid_order');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商品管理', 'mall', '', '', '1', '', '1', '', 'television', '0', '$parentid');");

            $parentid_goods=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商店设置', 'mall', 'ShopConfig', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商品列表', 'mall', 'Goods', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商品分类', 'mall', 'GoodsCategory', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商品品牌', 'mall', 'GoodsBrand', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('商品类型', 'mall', 'GoodsType', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('评价管理', 'mall', 'GoodsComment', 'index', '1', '', '1', '', '', '0', '$parentid_goods');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('财务管理', 'finance', '', '', '1', '', '1', '', 'money', '0', '$parentid');");

            $parentid_finance=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('提现管理', 'mall', 'CashOut', 'index', '1', '', '1', '', '', '0', '$parentid_finance');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('资金明细', 'mall', 'AccountLog', 'index', '1', '', '1', '', '', '0', '$parentid_finance');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('财务报表', 'mall', 'Settlement', 'index', '1', '', '1', '', '', '0', '$parentid_finance');");

            $parentid_settlement=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('数据报表', 'mall', 'Settlement', 'index', '1', '', '1', '', '', '0', '$parentid_settlement');");
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('提现统计', 'mall', 'CashOut', 'statistics', '1', '', '1', '', '', '0', '$parentid_settlement');");

        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
        
    }
    /**
     * 卸载前的一些操作
     */
    public function addonUninstallBefore(){
        try{
            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='订单管理' or name='商品管理' or name='财务管理' or name='财务报表'");
            if(!empty($result)){
                $parentid = [];
                foreach ($result as $key => $value) {
                    $parentid[] = $value['id'];
                }
                Db::execute("DELETE FROM `".DB_PREFIX."menu` WHERE id in (".implode(",", $parentid).") OR parentid in (".implode(",", $parentid).")");
                Db::execute("DELETE FROM `".DB_PREFIX."setting` WHERE sign='shop_config'");
            }
        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
    }
}
